package csce482.groupgraffiti.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.content.*;

public class Register extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.register);
	}
	
	// Validates and registers a new user
	public void submitRegistration(View view) {
		if (validateInfo()) {
			// Set the current user to the newly created user
			GroupGraffitiApp gga = (GroupGraffitiApp)getApplicationContext();
			EditText username = (EditText)findViewById(R.id.editTextRegisterUsername);
			gga.username = username.getText().toString();
			gga.loggedIn = true;
			
			// Go to the home screen
			Intent intent = new Intent(Register.this, Home.class);
			startActivity(intent);
		}
		else {
			return;
		}
	}
	
	// Ensures the registration info was entered correctly
	// TODO: add more checks
	private Boolean validateInfo() {
		EditText username = (EditText)findViewById(R.id.editTextRegisterUsername);
		EditText password = (EditText)findViewById(R.id.editTextRegisterPassword);
		EditText confirmPassword = (EditText)findViewById(R.id.editTextRegisterConfirmPassword);
		
		// Validate username
		if (username.getText().toString().equals("")) {
			Toast viewToast = Toast.makeText(this, "Username cannot be blank", Toast.LENGTH_SHORT);
			viewToast.show();
			return false;
		}
		
		// See if the passwords are the same
		if (!password.getText().toString().equals(confirmPassword.getText().toString())) {
			Toast viewToast = Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT);
			viewToast.show();
			return false;
		}
		
		// If all checks passed, allow the user to register
		return true;
	}

}
